
const {ccclass, property} = cc._decorator;

type heroState = {
    stand: number,
    attck: number
}

@ccclass
export default class hero extends cc.Component {

    // LIFE-CYCLE CALLBACKS:
    private State:heroState = {
        stand: 1,
        attck: 2
    }

    private Input:{} = { };
    // 连击
    private combo:number = 0;
    // 动画
    private anim:string = '';

    // 人物状态
    heroState:number = this.State.stand;
    // 人物速度
    @property(cc.Integer)
    speed:number = 0;
    // 人物方向
    sp:cc.Vec2 = cc.v2(0, 0);

    lv:cc.Vec2 = cc.v2(0, 0);

    aniHero:cc.Animation
    curAni: string = 'hero'

    onLoad () {
        // console.log(this.getComponent(cc.RigidBody).linearVelocity);
        this.aniHero = this.node.getChildByName("body").getComponent(cc.Animation);
        // 添加动画回调事件， 在动画结束后触发
        this.aniHero.on('finished', this.onAnimaFinished, this);
        
        // 初始化键盘输入监听
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this); 
    }
    // 键盘按下事件
    onKeyDown(event: cc.Event.EventKeyboard) {
        this.Input[event.keyCode] = 1;
        // 攻击按键按下
        if (this.heroState) {
            if (this.Input[cc.macro.KEY.j]) { 
                this.heroState = this.State.attck;
            }
        }
    }
    // 键盘抬起事件
    onKeyUp(event: cc.Event.EventKeyboard) {
        this.Input[event.keyCode] = 0;
        // 攻击按键抬起
        if (!this.Input[cc.macro.KEY.j]) {  
            this.heroState = this.State.stand;
        }
        if (event.keyCode == 68 || event.keyCode == 65 || event.keyCode == 39 || event.keyCode == 37) {
            this.anim = 'hero';
        }
    }

    onDestroy() {
        // 销毁键盘监听
        this.aniHero.off('finished', this.onAnimaFinished, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this); 
    }

    start () {

    }
    // 设置动画事件
    setAni(anima:string) {
        if (this.curAni == anima) return;
        this.curAni = anima;
        this.aniHero.play(this.curAni)
    }
    // 动画结束监听事件
    onAnimaFinished(event) {
        // 判断当前动画
        let name = this.aniHero.currentClip.name;
        
        if (name == 'attack' || name == 'attack2' || name == 'attack3') {
            this.anim = 'hero';
            
            this.heroState = this.State.stand;
            // this.setAni('hero');
            this.combo = (this.combo + 1) % 3;
        }
    }
    // 攻击
    attack() {
        if (this.Input[cc.macro.KEY.j]) { 
            if (this.combo == 0) {
                this.anim = 'attack';
            } else if (this.combo == 1) {
                this.anim = 'attack2';
            } else if (this.combo == 2) {
                this.anim = 'attack3';
            }
        }
    }
    // move
    move() {
        let scaleX = Math.abs(this.node.scaleX);
        if (this.Input[cc.macro.KEY.a] || this.Input[cc.macro.KEY.left]) {
            this.sp.x = -1;
            this.node.scaleX = -scaleX;
            this.anim = 'run';
        } else if (this.Input[cc.macro.KEY.d] || this.Input[cc.macro.KEY.right]) {
            this.sp.x = 1;
            this.node.scaleX = scaleX;
            this.anim = 'run';
        } else {
            this.sp.x = 0;
            // this.anim = 'hero';
        }   
    }

    // 移动事件
    moveState() {
        if (this.heroState == this.State.attck) {
            this.attack(); // 攻击
            this.sp.x = 0;
        } else if (this.heroState == this.State.stand) {
            // this.combo = 0;  // 连击重新开始
             this.move();
        }
        
        if (this.sp.x != 0) {
            this.lv.x = this.sp.x * this.speed;
        } else {
            this.lv.x = 0
        }
    }

    update (dt) {
        this.lv = this.node.getComponent(cc.RigidBody).linearVelocity;
        this.moveState();
        this.node.getComponent(cc.RigidBody).linearVelocity = this.lv;
        if(this.anim) {
            this.setAni(this.anim);
        }
        
        // this.setAni('run');
    }
}
